from werkzeug.security import generate_password_hash, check_password_hash
from app_db import db, UserMixin, login_manager


# 用户
# 加载用户函数
@login_manager.user_loader
def load_user(user_id):
    return UserInfo.query.filter_by(id = int(user_id)).first()


# 用户表
class UserInfo(UserMixin, db.Model):
    __tablename__ = 'user_info'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键ID')
    name = db.Column(db.String(60), nullable=False, comment='用户名')
    password_hash = db.Column(db.String(128), nullable=False, comment='登陆密码')
    registered_on = db.Column(db.Integer, nullable=False, comment='注册时间戳')

    def password(self, password=None):
        if password:
            self.password_hash = generate_password_hash(password)
        else:
            raise AttributeError('请输入密码!')

    def verify_password(self, passowrd):
        return check_password_hash(self.password_hash, passowrd)

    def get_id(self):
        return self.id

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False


# 通用维度表
class CommonCode(db.Model):
    """
    通用维度表
    """
    __tablename__ = 'common_code'

    id = db.Column(db.Integer, primary_key=True, comment='主健id')
    category_code = db.Column(db.String(20), nullable=False, comment='分类code')
    name = db.Column(db.String(50), nullable=False, comment='名称')
    is_valid = db.Column(db.Boolean, default=True, nullable=False, comment='1:有效;0:无效')

    __table_args__ = (db.UniqueConstraint('name', 'category_code', name='_index_name_category_code_unique'),)


# 收支类别表
class IncomeExpenseClass(db.Model):
    __tablename__ = 'income_expense_class'

    id = db.Column(db.Integer, primary_key=True, comment='主健id')
    type_name = db.Column(db.String(50), nullable=False, comment='类别名称')
    parent_id = db.Column(db.Integer, nullable=True, comment='父类id')
    type_id = db.Column(db.Integer, default=1, nullable=False,  comment='1：支出；2：收入')
    is_valid = db.Column(db.Boolean, default=True, nullable=False, comment='1:有效;0:无效')


# 账户表
class AccountType(db.Model):
    """
    账户表
    """
    __tablename__ = 'account_type'

    id = db.Column(db.Integer, primary_key=True, comment='主健id')
    type_id = db.Column(db.Integer, nullable=True, comment='账户类别id')
    account_name = db.Column(db.String(20), nullable=False, comment='账户名')
    user_id = db.Column(db.Integer, db.ForeignKey('user_info.id'), nullable=True, comment='所属用户id')
    is_valid = db.Column(db.Boolean, default=True, nullable=False, comment='1:有效;0:无效')


# index:流水账表
class AccountInfo(db.Model):
    """
    流水账
    """
    __tablename__ = 'account_info'

    id = db.Column(db.Integer, primary_key=True, comment='主健id')
    money = db.Column(db.DECIMAL(10, 2), nullable=False, comment='交易金额')
    income_expense_class_id = db.Column(db.Integer, nullable=False,index=True, comment='收支类别一级id')
    income_expense_id = db.Column(db.Integer, nullable=False, comment='收支类别二级id')
    income_expense_type_id = db.Column(db.Integer, default=1, nullable=False,  comment='1：支出；2：收入')
    pay_channel_id = db.Column(db.Integer, nullable=False, comment='支付方式/渠道id')
    account_class_id = db.Column(db.Integer, nullable=False, comment='支付来源id')
    account_id = db.Column(db.Integer,  nullable=False, comment='账户id')
    remark = db.Column(db.String(50), comment='备注')
    date = db.Column(db.Date, nullable=False, comment='交易时间戳')
    user_id = db.Column(db.Integer, nullable=True, index=True, comment='所属用户id')
    is_valid = db.Column(db.Boolean, default=True, nullable=False, comment='1:有效;0:无效')
